import { WXIconType } from "../../../wxAPI_";
import * as NS from "../../../Common/NetServices";
import { IsNull } from "../../../TYLibrary/Common/GlobalExtend";
import { Wx_XSPPage } from "../../../Common/Entitys";
import { Log } from "../../../TYLibrary/System/Logger";
import { Wx_PageData, WXDataClassRuntimeDecorator } from "../../../Common/EntityBase";

@WXDataClassRuntimeDecorator()
class ProductPageData extends Wx_PageData {
    public tabIndex: number = 0;
    public subTabIndex: number = 0;
    public bannerimgUrl: any = [
        "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_07.jpg",
        "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_08.jpg",
        "https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_09.jpg"
    ];
    public Current: number = 0;
    public ScrollTop: number = 0;
    public IsShowBackTop: boolean = false;
    public ImgIntroduceList: string = `<img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_06.jpg" class="img" />
        <img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_20.jpg" class="img" />
        <img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_26.jpg" class="img" />
        <img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_31.jpg" class="img" />`;
    public ImgParameterList: string =
        '<img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_23.jpg" class="img" />';
    public ImgPurchaseList: string =
        '<img src="https://www.e-taiyun.cn/XCXFiles/xsp/img/pic/pic_32.jpg" class="img" />';
    public day: number = 0;
    public hour: number = 0;
    public minute: number = 0;
    public second: number = 0;
    public IsShow: boolean = false;
    public Share: boolean = false;
    public IsDisable: boolean = true;
    public Count: number = 1;
    public ParameterIndex: number = 0;
    public ColorIndex: number = 0;
    public scrollHeight: number = 0;
}

class ProductPage extends Wx_XSPPage {
    private m_type = 0;
    public Data: ProductPageData = new ProductPageData();

    /**
     * 初始化页面信息
     * @param {any} options
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async InitData(options: any): Promise<void> {
        wx.getSystemInfo({
            success: res => {
                console.info(res.windowHeight);
                this.Data.scrollHeight = res.windowHeight;
                this.Data.ApplyChange(this);
            }
        });
        this._Timer(2018, 6, 5);
    }

    /**
     *
     * @param {*} e
     * @returns {Promise<void>}
     * @memberof IndexPage
     */
    public async ChangeHandler(e: any): Promise<void> {
        console.log("e", e.detail.current);
        let i = e.detail.current;
        this.Data.Current = i;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public scrolltolowerHandler(e: any): void {
        this.Data.tabIndex = 1;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public ShowSubTabHandler(e: any): void {
        const index = e.currentTarget.dataset.subtabindex;
        this.Data.subTabIndex = index;
        this.Data.ScrollTop = 0;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public ColorHandler(e: any): void {
        const index = e.currentTarget.dataset.index;
        this.Data.ColorIndex = index;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public ParameterHandler(e: any): void {
        const index = e.currentTarget.dataset.index;
        this.Data.ParameterIndex = index;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public GoBackTopHandler(): void {
        this.Data.ScrollTop = 0;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public ScrollHandler(e: any): void {
        if (e.detail.scrollTop > 300) {
            this.Data.IsShowBackTop = true;
        } else {
            this.Data.IsShowBackTop = false;
        }
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public AddShoppingHandler(e: any): void {
        this.m_type = e.currentTarget.dataset.type;
        this.Data.IsShow = true;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public bindShareListHandler(e: any): void {
        this.Data.Share = true;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public ConfirmHandler(e: any): void {
        this.CanlerHandler(e);
        wx.showToast({ title: "添加成功", icon: WXIconType.success });
        if (this.m_type != 0) {
            wx.navigateTo({ url: "../FirmOrder/Index" });
        }
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public CanlerHandler(e: any): void {
        this.Data.IsShow = false;
        this.Data.Share = false;
        this.Data.ApplyChange(this);
    }

    /**
     *
     * @param {*} e
     * @returns {void}
     * @memberof IndexPage
     */
    public NumberHandler(e: any): void {
        let type = e.currentTarget.dataset.type;
        if (type == 0) {
            if (this.Data.Count == 2) {
                this.Data.IsDisable = true;
                this.Data.Count = 1;
                this.Data.ApplyChange(this);
            } else {
                if (this.Data.Count > 1) this.Data.Count--;
                this.Data.ApplyChange(this);
            }
        } else {
            this.Data.Count++;
            this.Data.IsDisable = false;
            this.Data.ApplyChange(this);
        }
    }

    /**
     *
     * @private
     * @param {number} year
     * @param {number} month
     * @param {number} day
     * @memberof IndexPage
     */
    private _Timer(year: number, month: number, day: number): void {
        let time = setInterval(() => {
            let now = new Date();
            let endDate = new Date(year, month - 1, day);
            let leftTime = endDate.getTime() - now.getTime();
            let leftsecond = Math.floor(leftTime / 1000);
            let day1 = Math.floor(leftsecond / (60 * 60 * 24));
            let hour = Math.floor((leftsecond - day1 * 24 * 60 * 60) / 3600);
            let minute = Math.floor((leftsecond - day1 * 24 * 60 * 60 - hour * 3600) / 60);
            let second = Math.floor(leftsecond - day1 * 24 * 60 * 60 - hour * 3600 - minute * 60);
            this.Data.day = day1;
            this.Data.hour = hour;
            this.Data.minute = minute;
            this.Data.second = second;
            this.Data.ApplyChange(this);
        }, 1000);
    }
}

Page(new ProductPage());
